//
//  UIImage+Bundle.m
//  YSUtils
//
//  Created by Apple on 2020/6/1.
//  Copyright © 2020 Apple. All rights reserved.
//

#import "UIImage+Bundle.h"

@implementation UIImage (Bundle)

+ (instancetype)ys_imageNamed:(NSString *)name bundle:(NSString *)bundleName targetClass:(Class)targetClass {
    NSInteger scale = [[UIScreen mainScreen] scale];
    NSBundle *curB = [NSBundle bundleForClass:targetClass];
    NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", name,scale];
    NSString *dir = [NSString stringWithFormat:@"%@.bundle",bundleName];
    NSString *path = [curB pathForResource:imgName ofType:nil inDirectory:dir];
    if(path)
    {
        return [UIImage imageWithContentsOfFile:path];
    }
    else
    {
        imgName = [NSString stringWithFormat:@"%@.png", name];
        path = [curB pathForResource:imgName ofType:nil inDirectory:dir];
        if(path)
        {
            return [UIImage imageWithContentsOfFile:path];
        }
        else
        {
            imgName = [NSString stringWithFormat:@"%@@2x.png", name];
            path = [curB pathForResource:imgName ofType:nil inDirectory:dir];
            if(path)
            {
                return [UIImage imageWithContentsOfFile:path];
            }
            else
            {
                imgName = [NSString stringWithFormat:@"%@@3x.png", name];
                path = [curB pathForResource:imgName ofType:nil inDirectory:dir];
                if(path)
                {
                    return [UIImage imageWithContentsOfFile:path];
                }
                else
                {
                    return nil;
                }
            }
        }
    }
}

@end
